﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Common;
using RedHouse.Data.Entities;

namespace RedHouse.Data.Base
{
    public class CategoryService : ICategoryService
    {
        #region Declaration
        private NewRedHouseEntitiesDataContext _dataContext;
        #endregion

        private static ICategoryService _instance;
        public static ICategoryService Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new CategoryService();
                }
                return _instance;
            }
        }

        public CategoryService()
        {
            _dataContext = new NewRedHouseEntitiesDataContext();
        }

        #region ICategoryService Members
        public List<RedHouseCategory> GetAll()
        {
            List<RedHouseCategory> retList = new List<RedHouseCategory>();
            List<Category> categoryList = _dataContext.Categories.ToList();

            foreach (Category item in categoryList)
            {
                RedHouseCategory category = new RedHouseCategory();
                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == item.ID).FirstOrDefault();
                category.Id = item.ID;
                category.Title = detail.Title;
                category.LangId = detail.LangID.Value;
                category.Description = detail.Description;
                category.CreatedDate = item.CreatedDate.Value;
                category.CreatedBy = item.CreatedBy;
                category.Group = detail.GroupType.Value;
                category.ImageUrl = detail.ImageUrl;
                category.LocationId = item.LocationId.Value;
                category.DisplayOrder = detail.DisplayOrder.HasValue ? detail.DisplayOrder.Value : 0;
                
                retList.Add(category);
            }

            return retList;
        }

        public OperationResult Save(RedHouseCategory category, bool isNew)
        {
            OperationResult saveResult = new OperationResult()
            {
                Message = string.Empty,
                Result = true
            };
            try
            {
                Category savedCategory;
                Detail savedDetail;

                if (isNew)
                {
                    savedCategory = new Category();
                    savedDetail = new Detail();
                    savedCategory.ID = Guid.NewGuid();                    
                    savedDetail.ID = Guid.NewGuid();
                }
                else
                {
                    IDetailService detailService = new DetailService();
                    savedCategory = _dataContext.Categories.Single(c => c.ID == category.Id);
                    savedDetail = _dataContext.Details.Single(d => d.AssociatedID == category.Id);
                }

                savedCategory.CreatedDate = DateTime.Now;
                savedCategory.CreatedBy = "Admin";
                savedCategory.LocationId = category.LocationId;

                savedDetail.AssociatedID = savedCategory.ID;
                savedDetail.Description = category.Description;
                savedDetail.GroupType = category.Group;
                savedDetail.LangID = category.LangId;
                savedDetail.Title = category.Title;
                savedDetail.CreatedDate = DateTime.Now;
                savedDetail.ImageUrl = category.ImageUrl;
                savedDetail.CreatedBy = "admin";
                savedDetail.DisplayOrder = category.DisplayOrder;
                
                if (isNew)
                {
                    _dataContext.Categories.InsertOnSubmit(savedCategory);
                    _dataContext.Details.InsertOnSubmit(savedDetail);
                }

                _dataContext.SubmitChanges();
                saveResult.ReturnObject = category;
            }
            catch (Exception ex)
            {
                saveResult.Message = ex.Message;
                saveResult.Result = false;
            }
            return saveResult;
        }

        public RedHouseCategory GetCategoryByID(Guid id)
        {
            RedHouseCategory redHouseCategory = new RedHouseCategory();
            try
            {
                Category category = _dataContext.Categories.Where(c => c.ID == id).FirstOrDefault();
                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == id).FirstOrDefault();
                redHouseCategory.Id = id;
                redHouseCategory.Title = detail.Title;
                redHouseCategory.LangId = detail.LangID.Value;
                redHouseCategory.Description = detail.Description;
                redHouseCategory.CreatedDate = category.CreatedDate.Value;
                redHouseCategory.CreatedBy = category.CreatedBy;
                redHouseCategory.Group = detail.GroupType.Value;
                redHouseCategory.ImageUrl = detail.ImageUrl;
                redHouseCategory.LocationId = category.LocationId.Value;
                redHouseCategory.DisplayOrder = detail.DisplayOrder.HasValue ? detail.DisplayOrder.Value : 0;
            }
            catch (Exception ex)
            {
                return redHouseCategory;
            }
            return redHouseCategory;
        }

        public RedHouseCategory GetCategoryByName(string name)
        {
            RedHouseCategory redHouseCategory = new RedHouseCategory();
            try
            {
                Detail detail = _dataContext.Details.Where(d => d.Title.ToLower().Equals(name.ToLower())).FirstOrDefault();

                Category category = _dataContext.Categories.Where(c => c.ID == detail.AssociatedID).FirstOrDefault();
                redHouseCategory.Id = category.ID;
                redHouseCategory.Title = detail.Title;
                redHouseCategory.LangId = detail.LangID.Value;
                redHouseCategory.Description = detail.Description;
                redHouseCategory.CreatedDate = category.CreatedDate.Value;
                redHouseCategory.CreatedBy = category.CreatedBy;
                redHouseCategory.Group = detail.GroupType.Value;
                redHouseCategory.ImageUrl = detail.ImageUrl;
                redHouseCategory.LocationId = category.LocationId.Value;
                redHouseCategory.DisplayOrder = detail.DisplayOrder.HasValue ? detail.DisplayOrder.Value : 0;
            }
            catch (Exception ex)
            {
                return redHouseCategory;
            }
            return redHouseCategory;
        }

        public OperationResult Delete(RedHouseCategory category)
        {
            OperationResult deleteResult = new OperationResult()
            {
                Message = string.Empty,
                Result = true
            };
            try
            {

                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == category.Id).FirstOrDefault();
                _dataContext.Details.DeleteOnSubmit(detail);
                List<Dish> dishList = _dataContext.Dishes.Where(d => d.CategoryID.Value == category.Id).ToList();
                foreach (Dish dish in dishList)
                {
                    Detail dishDetail = _dataContext.Details.Where(d => d.AssociatedID == dish.ID).FirstOrDefault();
                    _dataContext.Details.DeleteOnSubmit(dishDetail);
                    List<Image> imageList = _dataContext.Images.Where(i => i.AssociatedID == category.Id).ToList();
                    _dataContext.Images.DeleteAllOnSubmit(imageList);
                }
                List<Image> categoryImages = _dataContext.Images.Where(i => i.AssociatedID == category.Id).ToList();
                _dataContext.Images.DeleteAllOnSubmit(categoryImages);
                _dataContext.Dishes.DeleteAllOnSubmit(dishList);
                _dataContext.SubmitChanges();
                deleteResult.ReturnObject = category;
            }
            catch (Exception ex)
            {
                deleteResult.Message = ex.Message;
                deleteResult.Result = false;
            }
            return deleteResult;
        }

        public OperationResult Delete(Guid id)
        {
            OperationResult deleteResult = new OperationResult()
            {
                Message = string.Empty,
                Result = true
            };
            try
            {

                Detail detail = _dataContext.Details.Where(d => d.AssociatedID == id).FirstOrDefault();
                _dataContext.Details.DeleteOnSubmit(detail);
                List<Dish> dishList = _dataContext.Dishes.Where(d => d.CategoryID.Value == id).ToList();
                foreach (Dish dish in dishList)
                {
                    Detail dishDetail = _dataContext.Details.Where(d => d.AssociatedID == dish.ID).FirstOrDefault();
                    _dataContext.Details.DeleteOnSubmit(dishDetail);
                    List<Image> imageList = _dataContext.Images.Where(i => i.AssociatedID == id).ToList();
                    _dataContext.Images.DeleteAllOnSubmit(imageList);
                }
                List<Image> categoryImages = _dataContext.Images.Where(i => i.AssociatedID == id).ToList();

                _dataContext.Images.DeleteAllOnSubmit(categoryImages);
                _dataContext.Dishes.DeleteAllOnSubmit(dishList);
                Category category = _dataContext.Categories.Where(c => c.ID == id).FirstOrDefault();
                _dataContext.Categories.DeleteOnSubmit(category);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                deleteResult.Message = ex.Message;
                deleteResult.Result = false;
            }
            return deleteResult;
        }

        #endregion

        #region ICategoryService Members


        public List<RedHouseCategory> GetCategoryByGroup(int groupId)
        {
            List<RedHouseCategory> retList = new List<RedHouseCategory>();
            try
            {
                List<Detail> details = _dataContext.Details.Where(d => d.GroupType==groupId).ToList();
                foreach (Detail item in details)
                {
                    Category category = _dataContext.Categories.Where(c => c.ID == item.AssociatedID).FirstOrDefault();
                    RedHouseCategory redHouseCategory = new RedHouseCategory();
                    redHouseCategory.Id = item.ID;
                    redHouseCategory.Title = item.Title;
                    redHouseCategory.LangId = item.LangID.Value;
                    redHouseCategory.Description = item.Description;
                    redHouseCategory.CreatedDate = item.CreatedDate.Value;
                    redHouseCategory.CreatedBy = item.CreatedBy;
                    redHouseCategory.Group = item.GroupType.Value;
                    redHouseCategory.ImageUrl = item.ImageUrl;
                    redHouseCategory.CategoryId = category.ID;
                    redHouseCategory.LocationId = category.LocationId.Value;
                    redHouseCategory.DisplayOrder = item.DisplayOrder.HasValue ? item.DisplayOrder.Value : 0;
                    retList.Add(redHouseCategory);
                }
            }
            catch (Exception ex)
            {
                return retList;
            }
            return retList;
        }

        #endregion
    }
}
